﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Data;
using System.Threading;
using System.Windows.Forms;

namespace KD.Sms.UI_Operate
{
    public class DataUpload
    {
        private static bool _bRun = false;
        private static Thread _thUpload;
        private static DataTable _tabSource;
        /// <summary>
        /// 是否需要上传
        /// </summary>
        public static bool E_NeedUpload { set; get; }

        /// <summary>
        /// 启动数据上传
        /// </summary>
        public static void DoStart()
        {
            _thUpload = new Thread(new ThreadStart(DoUpload)) { IsBackground = true };
            _bRun = true;
            E_NeedUpload = true;
            _thUpload.Start();
        }

        public static void DoStop()
        {
            _bRun = false;
            System.Threading.Thread.Sleep(500);
            if (_thUpload != null)
            {
                _thUpload.Abort();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        private static void DoUpload()
        {
            string _strLpr = string.Empty, _strFlowid, _strPostion, _strBatchDate, _strMine, _strMaterial, _strSupplier, _strTransport, _strMachine, _strBegintime, _strEndtime, _strRfid, _strPos, _strSparestr1;
            decimal _dTlen, _dTwidth, _dLen, _dWidth, _dHeight, _dWeight;
            int _iPail, _iPoscount, _iLayout, _iState, _iSamplingId;
            Image _imgBegin = null, _imgEnd = null;
            string _strImgBegin = string.Empty, _strImgEnd = string.Empty;
            System.Threading.Thread.Sleep(1000 * 10);
            while (_bRun)
            {
                if (E_NeedUpload)
                {
                    _tabSource = DAL.SqlLite.Main.Instance.DoQuerySamplingForUpload(false);
                    if (_tabSource != null && _tabSource.Rows.Count > 0)
                    {
                        foreach (DataRow dr in _tabSource.Rows)
                        {
                            try
                            {
                                _strMachine = CacheMemoryCenter.E_DeviceName;
                                _iSamplingId = Convert.ToInt32(dr["Sampling_id"]);
                                _strLpr = Convert.ToString(dr["Car_No"]);
                                _strFlowid = Convert.ToString(dr["FLowid"]);
                                _dTlen = Convert.ToDecimal(dr["TotalLength"]);
                                _dTwidth = 0;
                                _dLen = Convert.ToDecimal(dr["CarriageLength"]);
                                _dWidth = Convert.ToDecimal(dr["CarriageWidth"]);
                                _dHeight = Convert.ToDecimal(dr["CarriageHeight"]);
                                _strPostion = Convert.ToString(dr["CarriagePostions"]);
                                _strBatchDate = Convert.ToString(dr["batchdate"]);
                                _strMine = Convert.ToString(dr["kind_name"]);
                                _strMaterial = Convert.ToString(dr["Material_Name"]);
                                _strSupplier = Convert.ToString(dr["Supplier_Name"]);
                                _strTransport = Convert.ToString(dr["Unit_name"]);
                                _strSparestr1 = Convert.ToString(dr["Sparestr1"]);
                                _iPail = Convert.ToInt32(dr["Sample_Pail"]);
                                _iPoscount = Convert.ToInt32(dr["Pos_count"]);
                                _iLayout = Convert.ToInt32(dr["LayoutType"]);
                                _strBegintime = dr["Sampling_Time"] == DBNull.Value ? string.Empty : Convert.ToDateTime(dr["Sampling_Time"]).ToString("yyyy-MM-dd HH:mm:ss");
                                _strEndtime = dr["Sampling_Time_End"] == DBNull.Value ? string.Empty : Convert.ToDateTime(dr["Sampling_Time_End"]).ToString("yyyy-MM-dd HH:mm:ss");
                                _iState = Convert.ToInt32(dr["Sampling_State"]);
                                _dWeight = Convert.ToDecimal(dr["SampleWeight"]);
                                _strRfid = Convert.ToString(dr["SampleRfid"]);
                                _strPos = Convert.ToString(dr["SamplingPos"]);
                                _imgBegin = ImageHelper.DoGetImageFromFile(dr["Img_Begin"] == DBNull.Value ? string.Empty : dr["Img_Begin"].ToString());
                                _imgEnd = ImageHelper.DoGetImageFromFile(dr["Img_End"] == DBNull.Value ? string.Empty : dr["Img_End"].ToString());
                                _strImgBegin = _imgBegin == null ? string.Empty : ImageHelper.DoGetBase64FromImage(_imgBegin);
                                _strImgEnd = _imgEnd == null ? string.Empty : ImageHelper.DoGetBase64FromImage(_imgEnd);


                                //上传一条记录到三级编码系统
                                if (DAL.DAS.Main.Instance.DoUploadSamplingInfo(_strLpr, _strFlowid, _dTlen, _dTwidth, _dLen, _dWidth, _dHeight, _strPostion,
                                    _strBatchDate, _strMine, _strMaterial, _strSupplier, _strTransport,
                                    _strMachine, _iPail, _iPoscount, _iLayout, _strBegintime, _strEndtime, _iState, _dWeight, _strRfid, _strPos, _strImgBegin, _strImgEnd, _strSparestr1))
                                {
                                    if (DAL.SqlLite.Main.Instance.DoMarkUploadTarget(_iSamplingId))
                                    {
                                        FormHelper.ApplicationShow(UI.MsgType.Error, string.Format("车号{0} 采样数据上传编码系统成功!", _strLpr));
                                    }
                                }
                                System.Threading.Thread.Sleep(200);
                            }
                            catch (Exception ex)
                            {
                                FormHelper.ApplicationShow(UI.MsgType.Error, string.Format("往三级编码上传数据出现未知异常 车号:{0}", _strLpr));
                                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Interface, string.Format("往三级编码上传数据出现未知异常 车号:{0}", _strLpr));
                                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Interface, ex.ToString());
                            }
                        }
                    }
                    E_NeedUpload = false;

                }
                System.Threading.Thread.Sleep(1000);
            }
        }






    }
}
